package com.tuzhi.base.jpa.repository.listener;

import com.tuzhi.base.jpa.repository.BaseCommEntity;
import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.util.StringTzUtil;
import com.tuzhi.util.ThreadLocalContextHolder;
import com.tuzhi.util.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.aspectj.ConfigurableObject;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能说明：
 *
 * @author abu
 * @version 1.1.0
 * @date 2023/3/7
 */
@Slf4j
@Component
public class EntityListener implements ConfigurableObject {
    private final String LOG_PREFIX = "EntityListener 实体持久化监听>>>>>> HCompany实体执行";
    private final String LOG_POSTFIX = "<<<<<<";


    /**
     * insert前
     *
     * @param entity
     */
    @PrePersist
    public void PrePersist(BaseCommEntity entity) {
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
        }
        if (entity.getUpdateTime() == null) {
            entity.setUpdateTime(new Date());
        }

        entity.setSysStatus(0);
       /* String loginUser = ThreadLocalContextHolder.getUserInfo();
        if (!StringTzUtil.isBlankNull(loginUser)) {
            // log.info("设置创建人信息");
            entity.setCreateUser(loginUser);
        }*/
        LoginUserBO loginUser = UserContext.getUserInfo();
        if (StringUtils.isBlank(entity.getCreateUser()) && loginUser != null) {
            entity.setCreateUser(loginUser.getUserId().toString());
        }
        //log.info("{} [{}] {}", this.LOG_PREFIX, "新增操作之前", LOG_POSTFIX);
    }

    /**
     * insert后
     *
     * @param entity
     */
    @PostPersist
    public void PostPersist(final BaseCommEntity entity) {

        // log.info("{} [{}] {}", this.LOG_PREFIX, "新增操作之后", LOG_POSTFIX);
    }

    /**
     * update前
     *
     * @param entity
     */
    @PreUpdate
    public void PreUpdate(BaseCommEntity entity) {
        entity.setUpdateTime(new Date());
       /* String loginUser = ThreadLocalContextHolder.getUserInfo();
        if (!StringTzUtil.isBlankNull(loginUser)) {
            //log.info("设置修改人信息");
            entity.setUpdateUser(loginUser);
        }*/
        LoginUserBO loginUser = UserContext.getUserInfo();
        if (StringUtils.isBlank(entity.getUpdateUser()) && loginUser != null) {
            entity.setUpdateUser(loginUser.getUserId().toString());
        }
        // log.info("{} [{}] {}", this.LOG_PREFIX, "更新操作之前", LOG_POSTFIX);
    }

    /**
     * update后
     * @param entity
     */
/*    @PostUpdate
    public void PostUpdate(final BaseCommEntity entity) {
       // log.info("{} [{}] {}", this.LOG_PREFIX, "更新操作之后", LOG_POSTFIX);
    }*/

    /**
     * @param entity
     */
   /* @PreRemove
    public void PreRemove(final BaseCommEntity entity) {
        //log.info("{} [{}] {}", this.LOG_PREFIX, "删除操作之前", LOG_POSTFIX);
    }*/

    /**
     * @param entity
     */
 /*   @PostRemove
    public void PostRemove(final BaseCommEntity company) {
        //log.info("{} [{}] {}", this.LOG_PREFIX, "删除操作之后", LOG_POSTFIX);
    }*/

}
